package com.wang.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.common.utils.PageUtils;
import com.wang.common.utils.Query;
import com.wang.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.wang.gulimall.product.dao.AttrDao;
import com.wang.gulimall.product.dao.AttrGroupDao;
import com.wang.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.wang.gulimall.product.entity.AttrEntity;
import com.wang.gulimall.product.entity.AttrGroupEntity;
import com.wang.gulimall.product.service.AttrGroupService;
import com.wang.gulimall.product.vo.AttrGroupResVo;
import com.wang.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrDao attrDao;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryBycateID(Map<String, Object> params, Long cateId) {
        String key = (String) params.get("key");
        IPage<AttrGroupEntity> page = attrGroupDao.selectAttrEntityByCateId(
                new Query<AttrGroupEntity>().getPage(params), cateId, key);
        return new PageUtils(page);
    }

    /**
     * /product/attrgroup/attr/relation/delete
     * @param vos
     */
    @Override
    public void deleteAttrRelation(List<AttrVo> vos) {
        List<AttrAttrgroupRelationEntity> collect = vos.stream().map(attrVo -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attrVo, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrGroupDao.deleteRelation(collect);
    }

    /**
     * 获取属性分组没有关联的其他属性
     * @param attrgroupId
     * @param params
     * @return
     */
    @Override
    public PageUtils getAttrNoRelation(Long attrgroupId, Map<String, Object> params) {
        //1当前分组只能关联自己所属分类里面的属性
        //根据分组id查询该分组所属的分类id
        AttrGroupEntity attrGroupEntity = this.getById(attrgroupId);
        Long catelogId = attrGroupEntity.getCatelogId();

        //2当前分组只能关联别的分组没有引用的属性
        //2.1获取当前分类下的其他分组
        //查询当前分类id下的所有分组
        QueryWrapper<AttrGroupEntity> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("catelog_id", catelogId);
        List<AttrGroupEntity> attrGroups = this.baseMapper.selectList(wrapper1);

        //2.2这些分组关联的其他属性
        //将该分类下的所有分组集合中的每一项提取分组id成为新的集合
        List<Long> collect = attrGroups.stream().map(item -> {
            return item.getAttrGroupId();
        }).collect(Collectors.toList());
        //获得所有的关联关系
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper2 = new QueryWrapper<>();
        wrapper2.in("attr_group_id", collect);
        List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationDao.selectList(wrapper2);
        //获取了该分类下的所有分组下的所有已关联的属性ids
        List<Long> attrIDs = relationEntities.stream().map(item -> {
            return item.getAttrId();
        }).collect(Collectors.toList());
        //2.3从当前分类的所有属性中移除这些属性
        QueryWrapper<AttrEntity> wrapper3 = new QueryWrapper<>();
        //不查询属性类型为销售属性的属性
        wrapper3.ne("attr_type", 0);
        wrapper3.eq("catelog_id", catelogId);
        if (attrIDs != null && attrIDs.size() > 0) {
            wrapper3.notIn("attr_id", attrIDs);
        }
        String key = (String) params.get("key");
        if (key != null && key != "") {
            wrapper3.and(w -> {
                w.eq("attr_id", key).or().like("attr_name", key);
            });
        }
        IPage<AttrEntity> page = new Query<AttrEntity>().getPage(params);
        attrDao.selectPage(page, wrapper3);

        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupResVo> getAttrGropWitnAttrs(Long catelogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id", catelogId);
        //根据catelog_id在attrGroup表中查询出attrGroupEntities
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(queryWrapper);
        //流式编程 目标得到某分类下的所有分组，并且该分组携带了所有属于该分组的属性
        List<AttrGroupResVo> vos = attrGroupEntities.stream().map(item -> {
            AttrGroupResVo attrGroupResVo = new AttrGroupResVo();
            //将attrGroupEntities中每一项拷贝给attrGroupResVo
            BeanUtils.copyProperties(item, attrGroupResVo);
            QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("attr_group_id", item.getAttrGroupId());
            //根据attr_group_id在attrAttrgroupRelation表中查询relationEntities
            List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationDao.selectList(queryWrapper1);
            //在使用流式编程把relationEntities中的AttrId提取出来作为attrIDs
            List<Long> attrIDs = relationEntities.stream()
                    .map(relationEntity -> relationEntity.getAttrId())
                    .collect(Collectors.toList());
            QueryWrapper<AttrEntity> queryWrapper2 = new QueryWrapper<>();
            List<AttrEntity> attrEntities = new ArrayList<>();
            //todo 这里需优化，看着很难受(已优化)
            if (attrIDs != null && attrIDs.size() > 0) {
                //如果条件成立证明则根据attr_id在attr表中查attrEntities
                queryWrapper2.in("attr_id", attrIDs);
                attrEntities = attrDao.selectList(queryWrapper2);
            }
            //在attrGroupResVo中设置AttrEntities
            attrGroupResVo.setAttrs(attrEntities);
            return attrGroupResVo;
        }).collect(Collectors.toList());
        return vos;
    }

}